import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wanandroid_ohos/common/index.dart';
import 'package:flutter_wanandroid_ohos/pages/home/home_view.dart';
import 'package:flutter_wanandroid_ohos/pages/mine/mine_view.dart';
import 'package:flutter_wanandroid_ohos/pages/simple/simple_view.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold_ohos/common/index.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'tab_logic.dart';

class TabPage extends StatefulWidget {
  const TabPage({super.key});

  @override
  State<TabPage> createState() => _TabPageState();
}

class _TabPageState extends State<TabPage> {
  final TabLogic logic = Get.put(TabLogic());

  NavigationItemModel _buildNavigationBarItem({
    required IconData icon,
    required IconData activeIcon,
    required String title,
  }) {
    return NavigationItemModel(
      icon: icon,
      label: title,
      selectedIcon: activeIcon,
    );
  }

  Widget _buildNavigationBar() {
    return NavigationX(
      currentIndex: logic.currentIndex,
      onTap: (index) {
        logic.updateIndex(index);
      },
      items: [
        _buildNavigationBarItem(
          icon: TDIcons.home,
          activeIcon: TDIcons.home_filled,
          title: TextKey.tab_home.tr,
        ),
        _buildNavigationBarItem(
          icon: TDIcons.logo_github,
          activeIcon: TDIcons.logo_github_filled,
          title: TextKey.tab_simple.tr,
        ),
        _buildNavigationBarItem(
          icon: TDIcons.user,
          activeIcon: TDIcons.user_filled,
          title: TextKey.tab_mine.tr,
        ),
      ],
    ).elevation(2);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<TabLogic>(
        id: 'tab_view',
        builder: (logic) {
          return DoublePressBackWidget(
            child: Scaffold(
              backgroundColor: Colors.white,
              body: PageView(
                controller: logic.pageController,
                children: const [HomePage(), SimplePage(), MinePage()],
                onPageChanged: (index){
                  logic.updateIndex(index);
                },
                physics: const NeverScrollableScrollPhysics(),
              ),
              bottomNavigationBar: _buildNavigationBar(),
            ),
          );
        });
  }

  @override
  void dispose() {
    Get.delete<TabLogic>();
    super.dispose();
  }
}
